# Create your views here.

from django.shortcuts import render
import urllib2
import json
import pymysql

def post(url):
    request_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'}
    req = urllib2.Request(url,None,request_header)
    r = urllib2.urlopen(req)
    result = r.read()
    return result

def home(request):
    records=[]
    weibo = ''
    for i in range(9): 
        url= "http://m.weibo.cn/page/json?containerid=1005055796731205_-_WEIBO_SECOND_PROFILE_WEIBO&count=20"
        results = post(url)
        print results
        result = json.loads(results)
        f = result['cards'][0]['url']
        print f
        records.append(results);
        weibo = results
#         db = pymysql.connect('127.0.0.1','root','root','WeiboAD')
#         cursor = db.cursor()
#         sql = 'select * from weiboInfo'
#         cursor.execute("SET NAMES 'utf8';")
#         cursor.execute(sql)
#         results = cursor.fetchall()
#         records=[]
#         for result in list(results):
#             dic={}
#             dic['idstr']=result[0]
#     #         dic['id']=result[1]
#             records.append(dic);
#         print records
    return render(request,'home.html',{'string':weibo})